

import { game } from "../../../../script/game/control/game";
import { actionType, assetsType, cardType } from "../../../../script/game/gameType";
import { ListItem } from "../../../../script/util/ListView/ListItem";
import { ccs } from "../../../../script/util/services";
import { touchExd } from "../../../../script/util/TouchExtend";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends ListItem<assetsType> {
    
    //标题
    @property(cc.Label)
    title: cc.Label = null;

    //描述
    @property(cc.Label)
    desc: cc.Label = null;

    //影响
    @property(cc.Label)
    effect: cc.Label = null;

    //花费
    @property(cc.Label)
    price: cc.Label = null;

    //
    @property(cc.Label)
    payMonth: cc.Label = null;

    @property(cc.Label)
    leftMonth: cc.Label = null;

    @property(cc.Label)
    power: cc.Label = null;

    @property(cc.Sprite)
    icon: cc.Sprite = null;

    @property(cc.Node)
    have: cc.Node = null;

    @property(cc.Node)
    equip: cc.Node = null;

    @property(cc.Node) 
    canBuy: cc.Node  = null;

    start () {
        this.node.on('on_scroll_view', (msg) => {
            this.node.opacity = msg;
          });
    }
    initItem(data: assetsType, index: number) {
        
        if(data.type == "car" ){
            if(game.playerMgr.heroInfo.traffic) {
                if(data.id === game.playerMgr.heroInfo.traffic.id){
                    this.equip.active = true;
                }
            } else {
                this.equip.active = false;
            }
            if(data.id < 9 || game.playerMgr.bag.cardBag.getItem(data.type)){
                this.canBuy.active = false;
            } else { 
                this.canBuy.active = true;
            }
        }
        if(data.type == "house" ) {
            this.canBuy.active = false;
            if(game.playerMgr.heroInfo.house) {
                if(data.id == game.playerMgr.heroInfo.house.id) {
                    //已装备
                    this.equip.active = true;
                }else {
                    this.equip.active = false;
                }
            } else {
                this.equip.active = false;
            }
            
        }
        let assets = game.playerMgr.bag.assetsBag.getItem(data.id);
        
        if(assets) {
            this.have.active = true;
            data = assets.data;
        }else {
            this.have.active = false;
        }
        ccs.spriteExd.setSprite(this.icon.node, ccs.resMgr.getAssetsIcon(data.icon));
        this.title.string = data.name;
        this.desc.string = data.desc;
        this.price.string = "¥" + ccs.gf.getOmitNum(data.price) + "元";
        this.effect.string = ccs.gf.getEffectString(data.effect);
        this.power.string = "效率：" + (data.powerEffect < 0? "0%": ((1 - data.powerEffect) * 100).toFixed(1) + "%");
        this.payMonth.string = "月供：" + ccs.gf.getOmitNum(data.monthPay) + "元";
        this.leftMonth.string = "月数：" + ccs.gf.getOmitNum(data.month) + " 月"
        //如果背包有，显示背包的内容
        // 查看当前装备
    }
    onItemclick(data: assetsType) {
        ccs.winMgr.showWin('popBuyAssets', {id: data.id});
    }
}
